<template>
  <!-- 三日流失 -->
  <div class="app-container">
    <div class="app-wrap">
      <search-table ref="table" :table-loading="listLoading" :table-data="dataList" @query="getDataList(true)" @reset="refresh">
        <template v-slot:row>
          <el-form-item label="渠道:" label-width="50px">
            <el-select v-model="listQuery.agent_id" placeholder="请选择渠道" filterable >
              <el-option label="全部" value="" ></el-option>
              <el-option :label="'【'+item.value+'】'+item.label" :value="item.value" v-for="(item, index) in channelData" :key="index"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item  label="日期时间:" label-width="80px">
            <el-date-picker is-range v-model="listQuery.dateTime" type="daterange" range-separator="至" start-placeholder="开始日期" 
            end-placeholder="结束日期" class="full-width-input time-box" :default-time="['00:00:00', '23:59:59']" :clearable="false"></el-date-picker>
          </el-form-item>
        </template>
        <template v-slot:actions>
          <div class="statistic flexDetween" style="height:36px">
            <div class="flex">
              <el-button type="info" size="mini" icon="el-icon-refresh" @click="getDataList">刷新</el-button> 
              <el-button class="marginR20" type="success" size="mini" icon="el-icon-wallet" @click="getStatistical">统计</el-button>
              <div class="flex" v-show="statisticalData.reg_count">
                <p> 流失总人数：<span class="colorRed">{{ statisticalData.lose_count }}</span>人</p>
                <p> 时间内总玩家数量：<span class="colorRed">{{ statisticalData.reg_count }}</span></p>
              </div>
            </div>
            <el-button type="primary" size="mini" icon="el-icon-download" :disabled="!dataList.length" @click="getExport">导出</el-button> 
          </div>
        </template>
        <template>
          <el-table-column prop="user_id" label="用户ID" align="center" width="120"/>
          <el-table-column prop="nick_name" label="用户昵称" align="center" width="120"/>
          <el-table-column prop="agent_id" label="渠道名" align="center" width="200" show-overflow-tooltip>
            <template slot-scope="scope">
              <span><span v-show="scope.row.agent_id">【{{scope.row.agent_id}}】</span>{{ $public.channelLabel(scope.row.agent_id) }}</span>
            </template>
          </el-table-column>
          <el-table-column prop="is_bind_card" label="是否绑定银行卡" align="center" width="180" show-overflow-tooltip>
            <template slot-scope="scope">
              <el-tag :type="scope.row.is_bind_card==1?'success':'info'">{{ scope.row.is_bind_card==1?'绑定':'未绑定'}}</el-tag>
            </template>
          </el-table-column>
          <el-table-column prop="total_amount" label="剩余卢比" align="center"/>
          <el-table-column prop="cash_amount" label="可提现卢比" align="center"/>
          <el-table-column prop="no_cash_amount" label="不可提现" align="center"/>
          <el-table-column prop="create_time_str" label="注册时间" align="center" width="200"/>
          <el-table-column prop="login_time_str" label="登录时间" align="center" width="200"/>
        </template>
        <template v-slot:footer>
          <pagination :total="currentTotal" :page.sync="listQuery.page" :limit.sync="listQuery.size" @pagination="getDataList" />
        </template>
      </search-table>
    </div>
  </div>
</template>

<script>
import {
  throttle
} from '@/utils/index'
import {
  getRunOff,
  getRunOffStatistics,
  getRunOffExport
} from '@/api/statistics'
import SearchTable from '@/components/SearchTable'
export default {
  name: 'runOff',
  components: {
    SearchTable
  },
  data () {
    return {
      channelData:this.$store.state.commonState.channelDataArray,
      listQuery: {
        page: 1,
        size: 50,
        agent_id:'',
        dateTime: [this.$public.getDateWeek('s',0)+' 00:00:00',this.$public.getDateWeek('e',0)+' 23:59:59'],
        start_time: '',
        end_time: '',
      },
      statisticalData:{
        lose_count:0,
        reg_count:0,
      },
      listLoading: true,
      dataList: [],
      currentTotal: 0,
    }
  },
  created () {
    // this.getDataList();
  },
  activated(){
    this.channelData=this.$store.state.commonState.channelDataArray
    this.getDataList();
  },
  methods: {
    // 三日流失
    async getDataList (query) {
      let that = this,params=that.listQuery;
      if(query && !query.page){
        params.page=1
        params.size=50
      }
      that.dataList = []
      that.checkedItem = null
      if (params.dateTime && params.dateTime[0] && params.dateTime[1]) {
        params.start_time = that.$public.standardDate(params.dateTime[0])
        params.end_time = that.$public.standardDate(params.dateTime[1])
      }else{
        params.start_time = ''
        params.end_time = ''
      }
      const response = await  getRunOff(params)
      if (response.code == 1) {
        if(response.data.list){
          response.data.list.forEach(item=>{
            item.total_amount=that.$public.mathNumberDivideWan(item.total_amount)
            item.cash_amount=that.$public.mathNumberDivideWan(item.cash_amount)
            item.no_cash_amount=that.$public.mathNumberDivideWan(item.no_cash_amount)
          })
          that.dataList = response.data.list
          that.currentTotal = response.data.total
        }
      }else{
        that.$message.error(response.msg)
      }
      that.listLoading = false
    },
    // 统计
    getStatistical(){
      let that = this;
      getRunOffStatistics(that.listQuery).then(res => {
        if (res.code == 1) {
          that.statisticalData=res.data
        }
      }).catch((error) => {
        that.$message.error(error.msg)
      })
    },
    // 导出
    getExport(){
      let that = this,serveUrl=process.env.VUE_APP_BASE_API.substring(0,process.env.VUE_APP_BASE_API.length-6);
      getRunOffExport(that.listQuery).then(res => {
        if (res.code == 1) {
          window.open(serveUrl+res.data)
        }
      }).catch((error) => {
        that.$message.error(error.msg)
      })
    },
    // 刷新
    refresh: throttle(function () {
      this.listQuery = {
        page: 1,
        size: 50,
        agent_id:'',
        dateTime: [this.$public.getDateWeek('s',0)+' 00:00:00',this.$public.getDateWeek('e',0)+' 23:59:59'],
        start_time: '',
        end_time: '',
      }
      this.getDataList()
    }, 2000),
  }
}
</script>

<style lang="scss" scoped>

</style>
